/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/1/12 15:29
@File     : api.go
@Project  : gohub
@Desc     : 用于存放所有api路由
*/
package routes

import (
	"github.com/gin-gonic/gin"
	controllers "gohub/app/http/controllers/api/v1"
	"gohub/app/http/controllers/api/v1/auth"
	"gohub/app/http/middlewares"
	"gohub/pkg/config"
	"net/http"
)

// 注册网页相关路由
func RegisterApiRoutes(r *gin.Engine) {
	// 测试一个 v1 的路由组，我们所有的 v1 版本的路由都将存放到这里
	var v1 *gin.RouterGroup
	if len(config.Get("app.api_domain")) == 0 {
		v1 = r.Group("/api/v1")
	} else {
		//注册一个v1版本的路由组
		v1 = r.Group("/v1/")
	}

	//全局限流中间件：每小时限流。这里所有的api（根据IP）请求加起来。
	//作为参考 github api每小时最多60个请求（根据IP）。
	//测试时，可以调高一点
	v1.Use(middlewares.LimitIP("200-H"))
	{
		authGroup := v1.Group("/auth")
		//限流中间件：每小时限流
		authGroup.Use(middlewares.LimitIP("1000-H"))
		{
			suc := new(auth.SignupController)
			//判断手机号是否存在
			authGroup.POST("signup/phone/exist", suc.IsPhoneExist)
			//判断邮箱是否存在
			authGroup.POST("signup/email/exist", suc.IsEmailExist)
			//手机号码注册
			authGroup.POST("signup/using-phone", suc.SignupUsingPhone)
			//邮箱注册
			authGroup.POST("signup/using-email", suc.SignupUsingEmail)
			//验证码
			vcc := new(auth.VerifyCodeController)
			authGroup.POST("verify-codes/captcha", vcc.ShowCaptcha)
			authGroup.POST("verify-codes/phone", vcc.SendUsingPhone)
			authGroup.POST("verify-codes/email", vcc.SendUsingEmail)

			lgc := new(auth.LoginController)
			//使用手机号/验证码登录
			authGroup.POST("/login/using-phone", lgc.LoginByPhone)
			//支持手机号、邮箱、账号登录
			authGroup.POST("/login/using-password", lgc.LoginByPassword)
			//刷新access_token
			authGroup.POST("/login/refresh-token", lgc.RefreshToken)

			//重置密码
			pwc := new(auth.PasswordController)
			authGroup.POST("/password-reset/using-phone", pwc.ResetByPhone)
			authGroup.POST("/password-reset/using-email", pwc.ResetByEmail)

		}
		//用户模块
		uc := new(controllers.UsersController)
		v1.GET("/user", middlewares.AuthJWT(), uc.CurrentUser)
		usersGroup := v1.Group("/users")
		{
			usersGroup.GET("", uc.Index)
			usersGroup.PUT("", middlewares.AuthJWT(), uc.UpdateProfile)
			usersGroup.PUT("/email", middlewares.AuthJWT(), uc.UpdateEmail)
			usersGroup.PUT("/phone", middlewares.AuthJWT(), uc.UpdatePhone)
			usersGroup.PUT("/password", middlewares.AuthJWT(), uc.UpdatePassword)
			usersGroup.PUT("/avatar", middlewares.AuthJWT(), uc.UpdateAvatar)
		}
		//分类模块
		cgc := new(controllers.CategoriesController)
		cgcGroup := v1.Group("/categories")
		{
			cgcGroup.GET("", cgc.Index)
			cgcGroup.POST("", middlewares.AuthJWT(), cgc.Store)
			cgcGroup.PUT("/:id", middlewares.AuthJWT(), cgc.Update)
			cgcGroup.DELETE("/:id", middlewares.AuthJWT(), cgc.Delete)
		}
		//话题模块
		tpc := new(controllers.TopicsController)
		tpcGroup := v1.Group("/topics")
		{
			tpcGroup.GET("", tpc.Index)
			tpcGroup.POST("", middlewares.AuthJWT(), tpc.Store)
			tpcGroup.PUT("/:id", middlewares.AuthJWT(), tpc.Update)
			tpcGroup.GET("/:id", tpc.Show)
			tpcGroup.DELETE("/:id", middlewares.AuthJWT(), tpc.Delete)
		}
		//友情链接模块
		lsc := new(controllers.LinksController)
		linksGroup := v1.Group("/links")
		{
			linksGroup.GET("", lsc.Index)
		}

		v1.GET("/", func(c *gin.Context) {
			//以json格式响应
			c.JSON(http.StatusOK, gin.H{
				"hello": "goHub",
			})
		})
	}
}
